package com.lanou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.*;
import com.lanou.mapper.TbItemCatMapper;
import com.lanou.mapper.TbItemDescMapper;
import com.lanou.mapper.TbItemMapper;
import com.lanou.mapper.TbItemParamItemMapper;
import com.lanou.service.TbItemService;
import com.lanou.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbItemServiceImpl implements TbItemService {
    @Autowired
    public TbItemMapper tbItemMapper;
    @Autowired
    public TbItemDescMapper tbItemDescMapper;

    @Autowired
    public TbItemCatMapper tbItemCatMapper;
    @Autowired
    public TbItemParamItemMapper tbItemParamItemMapper;
//
    @Value("${ITEM_JSON_URL}")
    private String ITEM_JSON_URL;

    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        //分页处理
        PageHelper.startPage(page, rows);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(example);
        //获取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        //返回处理结果
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }

    @Override//查找所有
    public Map<String, Object> findAll() {
        TbItemExample tbItemExample = new TbItemExample();
        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", tbItems);
        return map;
    }

    @Override//添加商品
    public Map<String, Object> addItem(TbItem tbItem, String desc, String itemParams) {
        long id = new Date().getTime();
        tbItem.setId(id);
        Date created = new Date();
        Date updated = new Date();
        tbItem.setCreated(created);
        tbItem.setUpdated(updated);
        String title = tbItem.getTitle();
        tbItem.setTitle(title);
        tbItemMapper.insertSelective(tbItem);
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(id);
        tbItemDesc.setCreated(created);
        tbItemDesc.setUpdated(updated);
        tbItemDescMapper.insertSelective(tbItemDesc);
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(id);
        tbItemParamItem.setUpdated(updated);
        tbItemParamItem.setCreated(created);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItemMapper.insertSelective(tbItemParamItem);
        Map<String, Object> map = new HashMap<>();
        map.put("status", 200);
        HttpClientUtils.getObject(ITEM_JSON_URL+"?tbItem="+tbItem+"&desc="+desc+"&itemParams="+itemParams,Map.class);
        return map;
    }
}
